package project.mail;

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.UserDao;
import project.Domain.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static java.lang.System.out;


public class MailSender extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了login");
        try {
            processlogin(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //验证码
    public  String codeGenerator(Integer length) {
        char[] charArray = new char[length];
        short start = (short)'0';   //0的ASCII码是48
        short end = (short)'z';    //z的ASCII码是122（0到z之间有特殊字符）
        for (int i = 0; i < length; i++) {
            while(true)
            {
                char cc1 = (char)((Math.random()*(end-start))+start);
                if(Character.isLetterOrDigit(cc1))  //判断字符是否是数字或者字母
                {
                    charArray[i] = cc1;
                    break;
                }
            }
        }
        String StringRes = new String(charArray);//把字符数组转化为字符串
        return StringRes;
    }


    protected void processlogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception,JSONException {

        String mail_address = request.getParameter("email");
        request.setCharacterEncoding("UTF-8");
        out.println("main_address:" + mail_address);
        JSONObject json = new JSONObject();

        ////查看数据库
        UserDao userDao = new UserDao();
        List<User> list = userDao.getUserByEmail(mail_address);
        if(list.size()==0){
            json.put("result", 0);
            json.put("reason", "该邮箱未注册！");
        }
        else {
            json.put("result", 1);
            json.put("username", list.get(0).getUsername());
            json.put("password", list.get(0).getPassword());

            String verificationCode = codeGenerator(6);
            json.put("code", verificationCode);

            Mail mail = new Mail(mail_address, verificationCode);
            mail.verificationCode = verificationCode;
            mail.receiveMailAccount = mail_address;
            mail.send();
        }
        out.println(json);
        //将响应消息返回页面
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();


    }
}
